//
//  SHIAPVerifyManger.m
//  Supireader
//
//  Created by syh on 2020/6/2.
//  Copyright © 2020 syh. All rights reserved.
//

#import "SHIAPVerifyManger.h"
#import "SHIAPKeyChainStore.h"


@interface SHIAPVerifyManger ()

/** keychainStore */
@property(nonatomic, strong, nonnull) SHIAPKeyChainStore *keychainStore;

@end

NSString *const kBLPaymentVerifyManagerKeychainStoreServiceKey = @"com.supi.payment.models.keychain.store.service.key.www";

NSString *const kUserIdKey = @"userIdKey";

@implementation SHIAPVerifyManger



- (BOOL)paymentTransactionDidFinishFromServiceAndDeleteWhenExisted:(SKPaymentTransaction *)transaction {
    NSParameterAssert(transaction);
    if (!transaction) {
        return NO;
    }
    
//    NSArray<BLIAPTransactionOrder *> *models = [self.keychainStore bl_fetchAllPaymentTransactionModelsForUser:kUserIdKey error:nil];
    NSArray<BLIAPTransactionOrder *> *models = [self fetchAllPayment];

    if (!models.count) {
        return NO;
    }
    
    for (BLIAPTransactionOrder *model in models) {
        if ([model.transactionID isEqualToString:transaction.transactionIdentifier]) {//model.isTransactionValidFromService &&
            [self.keychainStore bl_deletePaymentTransactionModelWithTransactionIdentifier:transaction.transactionIdentifier forUser:kUserIdKey];
            return YES;
        }
    }
    return NO;
}

/** 删除一笔订单 */
- (BOOL)paymentDeleteWhenExisted:(BLIAPTransactionOrder *)order transaction:(SKPaymentTransaction *)transaction {
    
    if ([order.transactionID isEqualToString:transaction.transactionIdentifier]) {
        
        [self.keychainStore bl_deletePaymentTransactionModelWithTransactionIdentifier:transaction.transactionIdentifier forUser:kUserIdKey];
        return YES;
    }
    return NO;
}

/** 删除所有的  交易模型 */
- (void)delAllNotification
{
    if ([kUserIdKey length]) {
        [self.keychainStore bl_deleteAllPaymentTransactionModelsIfNeedForUser:kUserIdKey];
    }
}

- (void)appendPaymentTransactionModel:(BLIAPTransactionOrder *)transactionModel {
    if (!transactionModel) {
        return;
    }
    
    // 首先持久化到 keychain.
    [self.keychainStore bl_savePaymentTransactionModels:@[transactionModel] forUser:kUserIdKey];}

/** 指定交易标识的交易是否已经持久化到了 keychain 中了. */
- (BOOL)transactionDidStoreInKeyChainWithTransactionIdentifier:(NSString *)transactionIdentifier {
    if (!transactionIdentifier.length) {
        return NO;
    }
    
    NSArray<BLIAPTransactionOrder *> *models = [self.keychainStore bl_fetchAllPaymentTransactionModelsForUser:kUserIdKey error:nil];
    if (!models.count) {
        return NO;
    }

    for (BLIAPTransactionOrder *model in models) {
        if ([model.transactionID isEqualToString:transactionIdentifier]) {
//            if (model.sn.length && model.receiptData.length && model.transactionID.length) {
//                return YES;
//            }
//            [self.keychainStore bl_deletePaymentTransactionModelWithTransactionIdentifier:transactionIdentifier forUser:kUserIdKey];
            return YES;
        }
    }
    return NO;
}

/** 获取所有订单 */
- (NSMutableArray *)fetchAllPayment
{
    NSArray<BLIAPTransactionOrder *> *models = [self.keychainStore bl_fetchAllPaymentTransactionModelsForUser:kUserIdKey error:nil];

    NSMutableArray<BLIAPTransactionOrder *> * modelsM = [NSMutableArray array];
    for (BLIAPTransactionOrder *orderM in models) {
        if ([orderM.uid isEqualToString:objectUserId] || [orderM.uid isEqualToString:@"dtk"]) {
            [modelsM addObject:orderM];
        }
    }

    return modelsM;
}

/** 获取所有账号的未完成订单 */
- (NSArray *)fetchAllCountPayment
{
    NSArray<BLIAPTransactionOrder *> *models = [self.keychainStore bl_fetchAllPaymentTransactionModelsForUser:kUserIdKey error:nil];
    return models;
}

- (SHIAPKeyChainStore *)keychainStore
{
    if (_keychainStore == nil) {
        _keychainStore = [SHIAPKeyChainStore keyChainStoreWithService:kBLPaymentVerifyManagerKeychainStoreServiceKey];
    }
    return _keychainStore;
}

/** 改变某笔交易的验证状态. */
//- (void)updatePaymentTransactionModelStateWithTransactionIdentifier:(NSString *)transactionIdentifier {
//    if (!transactionIdentifier.length) {
//        return;
//    }
//
//    [self.keychainStore bl_updatePaymentTransactionModelStateWithTransactionIdentifier:transactionIdentifier isTransactionValidFromService:YES forUser:kUserIdKey];
//}

@end
